#include "common/config/config.h"
#include "common/log/log.h"

#include "user.h"

namespace F2FB
{

namespace Database
{

std::vector<unsigned> User::ids(
    sqlite::database& database
)
{
    std::vector<unsigned> result;
    database << "SELECT id FROM users;"
        >> [&result](unsigned id)
    {
        result.emplace_back(id);
    };
    return result;
}

std::vector<std::shared_ptr<Entity>> User::entities(
    sqlite::database& database
)
{
    const auto sqlStr = "SELECT "
                        "  id "
                        ", login "
                        ", first_name "
                        ", middle_name "
                        ", last_name "
                        ", email "
                        ", password_hash "
                        ", need_change_password "
                        ", more_info "
                        ", is_blocked "
                        "FROM users;";

    auto query = database << sqlStr;
    std::vector<std::shared_ptr<Entity>> result;
    query >>
        [&result](
            unsigned id,
            std::string login,
            std::string firstName,
            std::string middleName,
            std::string lastName,
            std::string email,
            std::string passwordHash,
            bool needChangePassword,
            std::string moreInfo,
            bool isBlocked
        )
    {
        auto entity = std::make_shared<User>();
        entity->m_data["id"] = id;
        entity->m_data["login"] = login;
        entity->m_data["first_name"] = firstName;
        entity->m_data["middle_name"] = middleName;
        entity->m_data["last_name"] = lastName;
        entity->m_data["email"] = email;
        entity->m_data["password_hash"] = passwordHash;
        entity->m_data["need_change_password"] = needChangePassword;
        entity->m_data["more_info"] = moreInfo;
        entity->m_data["is_blocked"] = isBlocked;
        result.push_back(entity);
    };
    return result;
}

} // namespace Database

} // namespace F2FB
